package com.hermes.comics.extractors;

import android.text.TextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.hermes.comics.data.Issue;
import com.hermes.comics.data.IssueTag;
import com.hermes.comics.data.Serie;
import com.hermes.comics.data.SerieTag;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class GoodReadsExtractor extends WebExtractor {

  private static final Pattern TITLE_PATTERN = Pattern.compile(
      "<h1 id=\"bookTitle\" class=\"bookTitle\" itemprop=\"name\">(.*?)</h1>", Pattern.DOTALL);

  private static final Pattern TITLE_ISSUE_PATTERN = Pattern.compile(
      "(.*?)<a.*?", Pattern.DOTALL);

  private static final Pattern TITLE_SERIE_PATTERN = Pattern.compile(
      "<a.*?>(.*?)</a>", Pattern.DOTALL);

  private static final Pattern AUTHOR_PATTERN = Pattern.compile(
      "<a href=\".*?\" class=\"authorName\" itemprop=\"url\"><span itemprop=\"name\">(.*?)</span></a>", Pattern.DOTALL);

  private static final Pattern PAGES_PATTERN = Pattern.compile(
      "<span itemprop=\"numberOfPages\">(\\d+) pages</span>", Pattern.DOTALL);

  private static final Pattern FORMAT_PATTERN = Pattern.compile(
      "<span itemprop=\"bookFormatType\">(.*?)</span>", Pattern.DOTALL);

  private static final Pattern PUBLISHER_PATTERN = Pattern.compile(
      "<div class=\"row\">\\s*Published.*?by(.*?)<", Pattern.DOTALL); 

  private static final Pattern DATE_PATTERN = Pattern.compile(
      "<div class=\"row\">\\s*Published(.*?)by.*?</div>", Pattern.DOTALL); 

  private static final Pattern IMAGE_PATTERN = Pattern.compile(
      "<img alt=\".*?\" id=\"coverImage\" src=\"(.*?)\" /></a>", Pattern.DOTALL); 
          
  public GoodReadsExtractor(String url, String content) {
    super(url, content);
  }

  @Override
  public boolean extractSerie(Serie serie) {
    String title = match(getContent(), TITLE_PATTERN);
    String titleFixed = match(title, TITLE_SERIE_PATTERN);
    serie.setTag(SerieTag.TITLE, titleFixed == null ? title.trim() : titleFixed.trim());
    serie.setTag(SerieTag.AUTHOR, TextUtils.join("|", matchAll(getContent(), AUTHOR_PATTERN)));
    serie.setTag(SerieTag.FORMAT, match(getContent(), FORMAT_PATTERN));
    serie.setTag(SerieTag.LANGUAGE, "en");
    serie.setTag(SerieTag.PUBLISHER, match(getContent(), PUBLISHER_PATTERN));

    System.out.println("--------------------- SERIE -----------------------------");
    System.out.println(serie.verbose());
    System.out.println("--------------------- SERIE -----------------------------");

    return true;
  }

  @Override
  public boolean extractIssue(Issue issue) {
    String title = match(getContent(), TITLE_PATTERN);
    if (TextUtils.isEmpty(title)) {
      title = "";
    }
    String titleFixed = match(title, TITLE_ISSUE_PATTERN);
    issue.setTag(IssueTag.TITLE, titleFixed == null ? title.trim() : titleFixed.trim());

    issue.setTag(IssueTag.URL, getUrl());
    issue.setTag(IssueTag.PAGES, match(getContent(), PAGES_PATTERN));
    issue.setTag(IssueTag.DATE, match(getContent(), DATE_PATTERN));
    issue.setTag(IssueTag.IMG, match(getContent(), IMAGE_PATTERN));

    System.out.println("--------------------- ISSUE -----------------------------");
    System.out.println(issue.verbose());
    System.out.println("--------------------- ISSUE -----------------------------");

    return true;
  }
}
